package com.engine.salary.entity.salaryacct.bo;

import cn.hutool.core.util.StrUtil;
import com.engine.salary.constant.TaxDeclarationDataIndexConstant;
import com.engine.salary.entity.salaryacct.po.SalaryAcctResultPO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.util.SalaryEntityUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class SalaryAcctConsolidatedTaxBO {

    /**
     * 处理合并计税的字段的值
     *
     * @param resultValue       本次薪资核算的结果
     * @param salaryItem        当前正在核算的薪资项目
     * @param salaryItems       系统内所有的薪资项目
     * @param salaryAcctResults 其他设计合并计税的薪资核算结果
     * @return
     */
    public static String handleConsolidatedTaxValue(String resultValue,
                                                    SalaryItemPO salaryItem,
                                                    List<SalaryItemPO> salaryItems,
                                                    List<SalaryAcctResultPO> salaryAcctResults) {
        // 社保福利的当月值都是0
        List<String> welfareDataIndexList = Lists.newArrayList(TaxDeclarationDataIndexConstant.ENDOWMENT_INSURANCE,
                TaxDeclarationDataIndexConstant.MEDICAL_INSURANCE,
                TaxDeclarationDataIndexConstant.UNEMPLOYMENT_INSURANCE,
                TaxDeclarationDataIndexConstant.HOUSING_PROVIDENT_FUND,
                TaxDeclarationDataIndexConstant.SOCIAL_SECURITY_TOTAL,
                TaxDeclarationDataIndexConstant.ACCUMULATION_FUND_TOTAL,
                TaxDeclarationDataIndexConstant.SPECIAL_DEDUCTION);
        if (welfareDataIndexList.contains(salaryItem.getCode())) {
            return BigDecimal.ZERO.toPlainString();
        }
        // 专项附加扣除的当月值都是0
        List<String> speAddiDeductionDataIndexList = Lists.newArrayList(TaxDeclarationDataIndexConstant.ADD_UP_CHILD_EDUCATION,
                TaxDeclarationDataIndexConstant.ADD_UP_HOUSING_LOAN_INTEREST,
                TaxDeclarationDataIndexConstant.ADD_UP_HOUSING_RENT,
                TaxDeclarationDataIndexConstant.ADD_UP_CONTINUING_EDUCATION,
                TaxDeclarationDataIndexConstant.ADD_UP_SUPPORT_ELDERLY,
                TaxDeclarationDataIndexConstant.ADD_UP_ILLNESS_MEDICAL,
                TaxDeclarationDataIndexConstant.ADD_UP_INFANT_CARE);
        if (speAddiDeductionDataIndexList.contains(salaryItem.getCode())) {
            return BigDecimal.ZERO.toPlainString();
        }
        // 本月（次）减除费用也是0
        if (TaxDeclarationDataIndexConstant.SUBTRACTION.equals(salaryItem.getCode())) {
            return BigDecimal.ZERO.toPlainString();
        }
        // 当前累计减除费用、当前累计专项扣除合计、当前累计专项附加扣除合计
        if (TaxDeclarationDataIndexConstant.ADD_UP_SUBTRACTION.equals(salaryItem.getCode())
                || TaxDeclarationDataIndexConstant.ADD_UP_SPECIAL_DEDUCTION.equals(salaryItem.getCode())
                || TaxDeclarationDataIndexConstant.ADD_UP_SPE_ADDI_DEDUCTION.equals(salaryItem.getCode())) {
            return salaryAcctResults.stream()
                    .filter(e -> Objects.equals(e.getSalaryItemId(), salaryItem.getId()))
                    .findFirst()
                    .map(SalaryAcctResultPO::getResultValue)
                    .orElse("");
        }
        // 当前累计收入 = 累计值 + 本次收入 + 上几次收入
        if (TaxDeclarationDataIndexConstant.ADD_UP_INCOME.equals(salaryItem.getCode())) {
            Optional<Long> optional = salaryItems.stream().filter(e -> StringUtils.equals(e.getCode(), TaxDeclarationDataIndexConstant.INCOME)).map(SalaryItemPO::getId).findAny();
            if (!optional.isPresent()) {
                return "";
            }
            BigDecimal income = salaryAcctResults.stream()
                    .filter(e -> Objects.equals(e.getSalaryItemId(), optional.get()))
                    .map(e -> SalaryEntityUtil.empty2Zero(e.getResultValue()))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            if (StrUtil.isBlank(resultValue)) {
                resultValue = "0.00";
            }
            return new BigDecimal(resultValue).add(income).toPlainString();
        }
        // 个税调差为0
        if (TaxDeclarationDataIndexConstant.TAX_ADJUSTMENT.equals(salaryItem.getCode())) {
            return BigDecimal.ZERO.toPlainString();
        }
        // 当前累计已扣缴税额合计 = 累计值 + 上几次预扣预缴个税 - 个税调差
        if (TaxDeclarationDataIndexConstant.ADD_UP_ADVANCE_TAX.equals(salaryItem.getCode())) {
            Optional<Long> optional = salaryItems.stream().filter(e -> StringUtils.equals(e.getCode(), TaxDeclarationDataIndexConstant.REFUNDED_OR_SUPPLEMENTED_TAX)).map(SalaryItemPO::getId).findAny();
            if (!optional.isPresent()) {
                return "";
            }
            BigDecimal refundedOrSupplementedTax = salaryAcctResults.stream()
                    .filter(e -> Objects.equals(e.getSalaryItemId(), optional.get()))
                    .map(e -> SalaryEntityUtil.empty2Zero(e.getResultValue()))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            // 个税调差
            Optional<Long> taxAdjustmentOptional = salaryItems.stream().filter(e -> StringUtils.equals(e.getCode(), TaxDeclarationDataIndexConstant.TAX_ADJUSTMENT)).map(SalaryItemPO::getId).findAny();
            BigDecimal taxAdjustment = BigDecimal.ZERO;
            if (taxAdjustmentOptional.isPresent()) {
                taxAdjustment = salaryAcctResults.stream()
                        .filter(e -> Objects.equals(e.getSalaryItemId(), taxAdjustmentOptional.get()))
                        .map(e -> SalaryEntityUtil.empty2Zero(e.getResultValue()))
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
            }
            if (StrUtil.isBlank(resultValue)) {
                resultValue = "0.00";
            }
            return new BigDecimal(resultValue).add(refundedOrSupplementedTax).subtract(taxAdjustment).toPlainString();
        }
        return resultValue;
    }
}
